/*
* @Author: dengjiayao
* @Date:   2017-08-29 14:08:25
 * @Last Modified by: songwh
 * @Last Modified time: 2018-02-03 16:29:09
*/
import Pages from '../../component/pages/pages.vue'
import NewShareConfig from '../new-share-config/new-share-config.vue'

import DatePicker from 'vue-datepicker'
import getDatePickerOption from '../../util/date-picker-option'

import {
  showMsg,
  getNewSharesList,
  updateNewShare,
  swapNewShare
} from '../../vuex/action'

export default {
  name: 'new-shares-list',
  components: {
    'vue-pages': Pages,
    'vue-config': NewShareConfig,
    'vue-date-picker': DatePicker
  },
  data() {
    return {
      list: [],
      pages: {
        start: 0,
        totalPages: 0,
        limit: 20
      },
      filter: {
        code: '',
        listedDate: '',
        endTime: ''
      },
      datePickerOption: getDatePickerOption({
        format: 'YYYY-MM-DD'
      })
    }
  },
  vuex: {
    getters: {
      fresh: state => state.hkListNeedFresh
    },
    actions: {
      xShowMsg: showMsg,
      xGetList: getNewSharesList,
      xUpdate: updateNewShare,
      xSwap: swapNewShare
    }
  },
  created() {
    this.getList({})
  },
  methods: {
    addItem() {
      let ref = this.$refs.newShareConfig
      ref.actionType = 'ADD'
      ref.form = {
        name: '',
        code: '',
        title: '',
        gateMoney: '',
        price: '',
        num: '',
        descr: '',
        deductMoneyTime: '',
        listedDate: '',
        listingPrice: '',
        endTime: '',
        url: '',
        isRecommend: '0',
        selectedSharesSpend: ''
      }
      ref.showModal = true
    },
    getList({ start = this.pages.start, limit = this.pages.limit }) {
      let params = {
        start: start,
        limit: limit
      }
      for(let k in this.filter){
        if (this.filter[k]) {
          params[k] = this.filter[k]
        }
      }
      this.xGetList(params)
        .then(data => {
          this.list = data.list
          this.pages.totalPages = Math.ceil(data.count / limit)
        })
        .catch(res => {
          this.xShowMsg({
            type: 'danger',
            content: res.error_msg
          })
        })
    },
    updateItem(it) {
      let ref = this.$refs.newShareConfig
      ref.actionType = 'UPDATE'
      ref.form = it
      ref.showModal = true
    },
    updateItemStatus(id, newStatus) {
      this.xUpdate({
        id: id,
        status: newStatus + ''
      }).catch(res => {
        this.xShowMsg({
          type: 'danger',
          content: res.error_msg
        })
      })
    },
    swap(id1, id2) {
      this.xSwap({
        fromId: id1,
        toId: id2
      }).catch(res => {
        this.xShowMsg({
          type: 'danger',
          content: res.error_msg
        })
      })
    },
    search() {
      this.pages.start = 0
      this.getList({})
    },
    clear(){
      this.filter = {
        code: '',
        listedDate: '',
        endTime: ''
      }
    }
  },
  watch: {
    fresh(val) {
      if (val) {
        this.getList({})
      }
    }
  }
}
